package com.aldobucchi.utils
{
	import mx.effects.AnimateProperty;
	
	import com.aldobucchi.core.dependency.DependencySpec;
	import com.aldobucchi.core.dependency.EventDispatcherWithDependenciesBase;

	public class NumberAnimator extends EventDispatcherWithDependenciesBase
	{


		public var duration:Number = 500;


		private var inputSpec:DependencySpec = new DependencySpec( "input", "inputChanged" );
		[Bindable("inputChanged")]
		public function get input( ):int
		{
			return dh.getDependencyValue( inputSpec );	
		}
		
		public function set input( v:int ):void
		{
			dh.setDependencyValue( inputSpec, v );
		}
		

		
	
	
		private var outputSpec:DependencySpec = new DependencySpec( "output", "outputChanged", null, false, 0 );
		[Bindable("outputChanged")]
		public function get output( ):int
		{
			return dh.getDependencyValue( outputSpec );	
		}
		public function set output( v:int ):void
		{
			dh.setDependencyValue( outputSpec, v );
		}	


		
		private var ap:AnimateProperty;		
		
		
		
		
		override protected function dependenciesUpdated( ):void
		{
			if ( dh.modifications[ inputSpec ] )
			{
				if ( ap )
				{
					if ( ap.isPlaying )
						ap.stop( );
				} 
				else
				{
					ap = new AnimateProperty( this );
					ap.property = "output";
					ap.duration = duration;
				}
				ap.fromValue 	= output;
				ap.toValue 		= input;

				
				var delta:Number = Math.abs( ap.fromValue - ap.toValue );
				if ( delta > 100 )
					ap.duration	= 1000;	// should be parametric	

				ap.play( );
			}					
		}
		
		
		
	}

}